#include "app_device.h"
#include "log/log.h"
#include "stdlib.h"
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include "app_pool.h"
#include "app_commonTime.h"
#define BUFFER_SIZE 1024

//向设备中写数据的任务函数
static int write_task_fun(void *arg){
     Device *device = (Device *)arg;

    // 从设备写缓存中读取出第一份完整数据
    char data[128];
    app_buffer_read(device->write_buffer, data, 3);
    app_buffer_read(device->write_buffer, data+3, data[1] + data[2]);
    int len = 3 + data[1] + data[2];
    // 在写入设备调用写的前置处理函数
    if (device->func->pre_write)
    {
        len = device->func->pre_write(device, data, len);
    }

    // 得到当前时间与上一次写入时间的时间差
    long time_distance = app_common_getCurrentTime() - device->last_write_time;
    // 如果时间差小于100毫秒, 休息缺少的时间
    if (time_distance < 100)
    {
        usleep((100 - time_distance) * 1000);
    }

    // 向设备写数据
    write(device->fd, data, len);
    device->last_write_time= app_common_getCurrentTime();
}

//发送消息的任务函数，在线程池的某个线程中执行
static int  send_task_fun(void *arg){
    Device *device = (Device *)arg;
    char data[128];
    app_buffer_read(device->read_buffer,data,3);
    app_buffer_read(device->read_buffer,data+3,data[1]+data[2]);
    int len = data[1]+data[2]+3;
    //发送消息给服务器
    if(device->func->send_msg){
        device->func->send_msg(device,data,len);
    }
}

//初始化设备
Device *app_device_init(char *filname){
    Device *device = malloc(sizeof(Device));
    device->filname = filname;
    device->fd = open(filname, O_RDWR|O_CREAT,0644);
    device->conn_type = DEV_GENERAL;
    device->dev_type = DEV_GENERAL;
    device->write_buffer = app_buffer_init(BUFFER_SIZE);
    device->read_buffer = app_buffer_init(BUFFER_SIZE);
    device->running = 0;
    device->func = malloc(sizeof(DeviceFunc));
    return device;
}

static void *read_thread_fun(void *arg){
    Device *device = (Device *)arg;

    char data[128];
    while (device->running==1)
    {
        memset(data,0,sizeof(data));
       int len =read(device->fd,data,sizeof(data));
       //调用post_read函数对读取到的数据data后置处理
       if (device->func->post_read)
       {
           len = device->func->post_read(device,data,len);
       }
       app_buffer_write(device->read_thread,data,len);
       //注册发送消息的任务

       app_pool_registerTask(send_task_fun,device);
       
    }
    
}
//启动设备
int app_device_start(Device *device){
    
    if(device->running == 1){
        log_debug("device is running");
        return 0;
    }
    device->running = 1;
   if ( pthread_create(&device->read_thread,NULL,read_thread_fun,device)!=0)
   {
       log_error("create read thread failed");
       device->running = 0;
       return -1;
   }
   return 0;
}

//停止设备
int app_device_stop(Device *device){
    if (device->running == 0)
    {
        log_debug("device is not running");
        return 0;
    }
    pthread_cancel(device->read_thread);
    pthread_join(device->read_thread,NULL);
    device->running = 0;

    return 0;
    
}
//关闭释放设备
void app_device_close(Device *device){

    app_message_free(device->write_buffer);
    app_message_free(device->read_buffer);

    free(device->func);
    free(device);
}

/**
 * 接收处理数据
 */
int app_device_recvMessage(Device *device, char *data, int len)
{
    // 将接收的消息数据保存到设备写缓存中
    app_buffer_write(device->write_buffer, data, len);
    // 注册一个写任务来完成后缀将数据写入设备的操作
    
    return app_pool_registerTask(write_task_fun, device);
}